package com.example.simple;

/**
 * 简单的计算器服务
 * 演示IoC和AOP的概念
 */
public class SimpleCalculatorService {
    
    /**
     * 加法运算 - 带缓存
     */
    public double add(double a, double b) {
        String cacheKey = SimpleCache.generateKey("SimpleCalculatorService", "add", a, b);
        
        // 检查缓存
        Object cachedResult = SimpleCache.get(cacheKey);
        if (cachedResult != null) {
            return (Double) cachedResult;
        }
        
        // 执行计算
        System.out.println("执行加法运算: " + a + " + " + b);
        try {
            Thread.sleep(100); // 模拟计算延迟
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        double result = a + b;
        
        // 存入缓存
        SimpleCache.put(cacheKey, result, 30);
        
        return result;
    }
    
    /**
     * 减法运算 - 带缓存
     */
    public double subtract(double a, double b) {
        String cacheKey = SimpleCache.generateKey("SimpleCalculatorService", "subtract", a, b);
        
        // 检查缓存
        Object cachedResult = SimpleCache.get(cacheKey);
        if (cachedResult != null) {
            return (Double) cachedResult;
        }
        
        // 执行计算
        System.out.println("执行减法运算: " + a + " - " + b);
        try {
            Thread.sleep(100); // 模拟计算延迟
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        double result = a - b;
        
        // 存入缓存
        SimpleCache.put(cacheKey, result, 30);
        
        return result;
    }
    
    /**
     * 乘法运算 - 无缓存，演示日志
     */
    public double multiply(double a, double b) {
        System.out.println("开始执行乘法运算: " + a + " * " + b);
        long startTime = System.currentTimeMillis();
        
        try {
            Thread.sleep(150); // 模拟计算延迟
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        double result = a * b;
        
        long endTime = System.currentTimeMillis();
        System.out.println("乘法运算完成: " + a + " * " + b + " = " + result + ", 耗时: " + (endTime - startTime) + "ms");
        
        return result;
    }
    
    /**
     * 除法运算 - 演示异常处理
     */
    public double divide(double a, double b) {
        System.out.println("开始执行除法运算: " + a + " / " + b);
        long startTime = System.currentTimeMillis();
        
        if (b == 0) {
            System.out.println("除法运算异常: 除数不能为零");
            throw new ArithmeticException("除数不能为零");
        }
        
        try {
            Thread.sleep(120); // 模拟计算延迟
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        double result = a / b;
        
        long endTime = System.currentTimeMillis();
        System.out.println("除法运算完成: " + a + " / " + b + " = " + result + ", 耗时: " + (endTime - startTime) + "ms");
        
        return result;
    }
    
    /**
     * 幂运算 - 带缓存
     */
    public double power(double base, double exponent) {
        String cacheKey = SimpleCache.generateKey("SimpleCalculatorService", "power", base, exponent);
        
        // 检查缓存
        Object cachedResult = SimpleCache.get(cacheKey);
        if (cachedResult != null) {
            return (Double) cachedResult;
        }
        
        // 执行计算
        System.out.println("执行幂运算: " + base + " ^ " + exponent);
        try {
            Thread.sleep(200); // 模拟计算延迟
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        double result = Math.pow(base, exponent);
        
        // 存入缓存
        SimpleCache.put(cacheKey, result, 60);
        
        return result;
    }
    
    /**
     * 阶乘运算 - 带缓存
     */
    public long factorial(int n) {
        String cacheKey = SimpleCache.generateKey("SimpleCalculatorService", "factorial", n);
        
        // 检查缓存
        Object cachedResult = SimpleCache.get(cacheKey);
        if (cachedResult != null) {
            return (Long) cachedResult;
        }
        
        // 执行计算
        System.out.println("执行阶乘运算: " + n + "!");
        
        if (n < 0) {
            System.out.println("阶乘运算异常: 阶乘不能计算负数");
            throw new IllegalArgumentException("阶乘不能计算负数");
        }
        
        if (n == 0 || n == 1) {
            return 1;
        }
        
        try {
            Thread.sleep(300); // 模拟计算延迟
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        long result = 1;
        for (int i = 2; i <= n; i++) {
            result *= i;
        }
        
        // 存入缓存
        SimpleCache.put(cacheKey, result, 120);
        
        return result;
    }
}
